import { glSDKMgr } from "../../lib/GLSDKMgr";
import { GCtrl } from "../../lib/util/GCtrl";

const { ccclass, property } = cc._decorator;

@ccclass
export default class DWC_ExchangeCode extends cc.Component {

    @property(cc.EditBox)
    codeEdit: cc.EditBox = null;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    onEnable() {
        this.onShow();
    }
    onDisable() {
        this.onHide();
    }


    start() {

    }

    // update (dt) {}
    init(args) {

    }
    private exchangeReq() {
        let cmd = "";
        let msg: any = {
            exchange_code: this.codeEdit.string
        }
        GCtrl.GLUtils.sendRequest(cmd, msg, (error, data) => {
            if (data.errcode)
                GCtrl.GLUtils.showTips(data.errcodedes);
            else {
                GCtrl.GLUtils.showTips("兑换成功");
                this.onClickBack();
            }
        });
    }
    onClickBack() {
        GCtrl.UIMgr.close(GCtrl.GLConfig.DWC_ExchangeCode_path, GCtrl.UIMgr.closeType.hide);
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
    }
    onClickPasteCode() {
        //读取剪切板
        let str = glSDKMgr.getClipboard();
        this.codeEdit.string = str + "";
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
    }
    onClickExchange() {
        this.exchangeReq();
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
    }
    onShow() {

    }
    onHide() {
        this.codeEdit.string = "";
    }
}
